//
//  PhotoCollectionViewCell.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/30.
//

#import "PhotoCollectionViewCell.h"

@interface PhotoCollectionViewCell ()

@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UIButton *deleBtn;

@end

@implementation PhotoCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    [self.contentView addSubview:self.imageV];
    [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self.contentView addSubview:self.deleBtn];
    [self.deleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-4);
        make.top.mas_equalTo(4);
        make.width.height.mas_equalTo(20);
    }];
}

- (void)setImage:(UIImage *)image {
    _image = image;
    self.imageV.image = image;
    self.deleBtn.hidden = YES;
}

- (void)setImageUrl:(NSString *)imageUrl {
    _imageUrl = imageUrl;
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"avatar"]];
    self.deleBtn.hidden = NO;
}

- (UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
        _imageV.contentMode = UIViewContentModeScaleAspectFill;
        _imageV.clipsToBounds = YES;
        _imageV.layer.cornerRadius = 10;
    }
    return _imageV;
}

- (UIButton *)deleBtn {
    if (!_deleBtn) {
        _deleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleBtn setImage:[UIImage imageNamed:@"icon_del"] forState:UIControlStateNormal];
        _deleBtn.hidden = YES;
        @weakify(self)
        [[_deleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.deleteVideoBlock) {
                self.deleteVideoBlock(self.imageUrl);
            }
        }];
    }
    return _deleBtn;
}

@end
